<template>
  <div class="SignIncontainer">
    <h1>注册您的账号</h1>
    <el-form style="width: 500px" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios"
export default {
   data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          password: '',
          checkPass: '',
          username: '',
          email: "",
          phone: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
   },
   methods: {
      // 需要在后端的用户JSON数据中添加该账号密码 ， 然后需要在用户信息中添加该用户的信息
       
      async submitForm(formName) {
        let obj = {};
        obj.username = this.ruleForm.username;
        obj.phone = this.ruleForm.phone;
        obj.password = this.ruleForm.password;
        obj.email = this.ruleForm.email;
        // obj.username = this.ruleForm.username;
        console.log(formName);
        console.log(obj);
        console.log(this.ruleForm.username);
        if(this.ruleForm.password === this.ruleForm.checkPass) {
          let result = await this.$axios.post("/users/register", obj)
          console.log(result);
          if(result.data.code == 200) {
            console.log("此时注册成功了");
            // 为什么页面跳转之后会保留原来的样式 ， 就是说浏览器在页面跳转的时候会保留之前的样式 ， 然后如果跳转时候页面出现了相同的类名或者是id ， 那么该样式将会被直接套用
            this.$router.push({name: "HelloWorld"});
          }
        }
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style>
  .SignIncontainer {
    width: 100%;
    height: 100vh;
    display: flex;
    /* justify-content: center; */
    align-items: center;
    flex-direction: column;
  }
</style>